//
//  ColorLog.swift
//  SwiftWB
//
//  Created by luozhijun on 16/8/29.
//  Copyright © 2016年 luozhijun. All rights reserved.
//

import UIKit

//MARK: - 自定义log
func debugLog(items: Any..., file: String = #file, line: Int = #line) {
    #if DEBUG
        let shortCutFileName = (file as NSString).lastPathComponent
        print("===[\(shortCutFileName)]--[line:\(line)]👇:\n\(items)")
    #endif
}

//MARK: - 带颜色输出
struct ColorLog {
    // 决定颜色输出的标识
    static let ESCAPE = "\u{001b}["
    // 决定前景色还是背景色
    static let RESET_FG = ESCAPE + "fg;" // Clear any foreground color
    static let RESET_BG = ESCAPE + "bg;" // Clear any background color
    static let RESET = ESCAPE + ";"   // Clear any foreground or background color
    
    
    static func debugColorLog(colorLog: String, file: String = #file, line: Int = #line) {
        #if DEBUG
            let shortCutFileName = (file as NSString).lastPathComponent
            let locationString = ">>[\(shortCutFileName)]--[line:\(line)]👇:"
//            print("\(ESCAPE)fg230,44,73;\(string)\(RESET)")
            print(locationString)
            print("---" + colorLog + "---")
        #endif
    }
    
    /** 警告输出 */
    static func warn(message: String, file: String = #file, line: Int = #line) {
        debugColorLog("\(ESCAPE)fg186,45,162;\("Warning: " + message)\(RESET)", file: file, line: line)
    }
    
    /** 事件发生提醒 */
    static func eventTriggered(file: String = #file, line: Int = #line, function: String = #function) {
        debugColorLog("\(ESCAPE)fg0,0,255;\(function)\(RESET)", file: file, line: line)
    }
    
    // 红色输出
    static func red(items: Any..., file: String = #file, line: Int = #line) {
        debugColorLog("\(ESCAPE)fg230,44,73;\(items)\(RESET)", file: file, line: line)
    }
    
    // 绿色输出
    static func green(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            let scanner = NSScanner(string: "0x1f448")
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            let emoji = "\(Character(UnicodeScalar(result)))"
            debugColorLog(emoji+"\(ESCAPE)fg38,173,97;\(items)\(RESET)", file: file, line: line)
        #endif
    }
    
    // 蓝色输出
    static func blue(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            debugColorLog("\(ESCAPE)fg0,0,255;\(items)\(RESET)", file: file, line: line)
        #endif
    }
    
    //黄色输出
    static func yellow(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            debugColorLog("\(ESCAPE)fg242,196,15;\(items)\(RESET)", file: file, line: line)
        #endif
    }
    
    //紫色输出
    static func purple(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            debugColorLog("\(ESCAPE)fg255,0,255;\(items)\(RESET)", file: file, line: line)
        #endif
    }
    
    //青色输出
    static func cyan(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            debugColorLog("\(ESCAPE)fg0,255,255;\(items)\(RESET)", file: file, line: line)
        #endif
    }
    
    // 打印两个对象分别蓝色和黄色输出
    static func blueAndYellow<T>(obj1: T, obj2: T, file: String = #file, line: Int = #line) {
        #if DEBUG
            debugColorLog("\(ESCAPE)fg0,0,255;\(obj1)\(RESET)" + "\(ESCAPE)fg255,255,0;\(obj2)\(RESET)", file: file, line: line)
        #endif
    }
    
    // 亮蓝色输出
    static func lightBlue(items: Any..., file: String = #file, line: Int = #line) {
        #if DEBUG
            let scanner = NSScanner(string: "0x1f449")
            var result: UInt32 = 0
            scanner.scanHexInt(&result)
            let emoji = "\(Character(UnicodeScalar(result)))"
            debugColorLog(emoji+"\(ESCAPE)fg41,128,185;\(items)\(RESET)", file: file, line: line)
        #endif
    }
}
